package com.javafx07.controller;

import com.javafx07.Main;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PhotoController {

    @FXML
    private GridPane gridPane;

    private ImageView selectedImage;

    private List<ImageView> list = new ArrayList<>();

    public void deleteImage() {
        if (selectedImage != null) {
            list.remove(selectedImage);
            gridPane.getChildren().clear();
            for (ImageView view : list) {
                int size = gridPane.getChildren().size();
                int row = size / 4;
                int col = size % 4;
                gridPane.add(view, col, row);
            }
            selectedImage = null;
        }
    }


    @FXML
    public void addImage() {
        FileChooser chooser = new FileChooser();
        chooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                new FileChooser.ExtensionFilter("PNG", "*.png"));
        File file = chooser.showOpenDialog(null);
        if (file != null) {
            //  <ImageView fitHeight="114.0" fitWidth="199.0" pickOnBounds="true" preserveRatio="true" />
            ImageView view = new ImageView();
            view.setFitHeight(114.0);
            view.setFitWidth(199);
            view.setImage(new Image("file:" + file.getAbsolutePath()));
            ContextMenu contextMenu = new ContextMenu();
            MenuItem item = new MenuItem("删除");
            contextMenu.getItems().add(item);
            item.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    deleteImage();
                }
            });
            view.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
                @Override
                public void handle(ContextMenuEvent event) {
                    System.out.println("=====================");
                    contextMenu.show(view, event.getScreenX(), event.getScreenY());
                    selectedImage = view;
                }
            });

            view.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton() == MouseButton.PRIMARY) {
                        ResizeController.setImage(view.getImage());
                        Main.newStageTo("view/resize.fxml");
                    }

                }
            });

            ObservableList<Node> children = gridPane.getChildren();
            int size = children.size();
            int row = size / 4;
            int col = size % 4;
            list.add(view);
            gridPane.add(view, col, row);
        }
    }

}
